package com.lanou.test;

import com.lanou.entity.Item;
import com.lanou.entity.ItemExample;
import com.lanou.mapper.ItemMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class test1 {
    public static void main(String[] args) throws Exception{
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        File configFile = new File("/Users/dllo/Desktop/课堂笔记/0704/MBG01/src/generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);

    }
    @Test
    public void test1() throws FileNotFoundException {
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
                .build(new FileInputStream("/Users/dllo/Desktop/课堂笔记/0704/MBG01/src/mybatis.xml"));
        System.out.println(sqlSessionFactory);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //用或者连接条件,可以设置一个主条件,分条件用c2接一下,并且用or连接一下
        ItemMapper itemMapper = sqlSession.getMapper(ItemMapper.class);
        ItemExample itemExample = new ItemExample();
        itemExample.createCriteria().andTitleLike("%阿尔卡特%");//名字像阿尔卡特,并且价格大于一万,直接在后面点
        ItemExample.Criteria c2 = itemExample.createCriteria().andPriceGreaterThan(10000L);
        itemExample.or(c2);

        itemExample.setOrderByClause("price desc");//通过价格倒序排列

//        Item item = itemMapper.selectByPrimaryKey(635906l);
//        System.out.println(item.toString());
        List<Item> items = itemMapper.selectByExample(itemExample);
        for (Item item : items) {
            System.out.println(item.toString());
        }
//        System.out.println(items);
        Item item = new Item();
        item.setTitle("afdasf");
        item.setPrice(199L);
        //insert into item(id,title,price....) values(null,"",199,null,....)
        int i = itemMapper.insert(item);
        //哪个列有值就插入哪列,增加数据与修改数据用下面的方法
        int i1 = itemMapper.insertSelective(item);

    }
}
